package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.SysEmailHistory;

public class SysEmailHistoryRowMapper implements RowMapper<SysEmailHistory> {
    @SuppressWarnings("unused")
    private int type;

    public static final int TYPE_UID = 10;

    public SysEmailHistoryRowMapper() {
    }

    public SysEmailHistoryRowMapper(int type) {
        this.type = type;
    }
    @Override
    public SysEmailHistory mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseSysEmailHistory(new SysEmailHistory(), rs);
    }

    public static SysEmailHistory parseSysEmailHistory(SysEmailHistory sn, ResultSet rs) throws SQLException {
        return parseSysEmailHistory(sn, rs, null);
    }

    public static SysEmailHistory parseSysEmailHistory(SysEmailHistory sn, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            sn.setEid(rs.getLong(su + "eid"));
        } catch (SQLException e) {
            sn.setEid(null);
        }
        try {
            sn.setToEmail(rs.getString(su + "toEmail"));
        } catch (SQLException e) {
            sn.setToEmail(null);
        }
        try {
            sn.setValue(rs.getString(su + "value"));
        } catch (SQLException e) {
            sn.setValue(null);
        }
        try {
            sn.setType(rs.getShort(su + "type"));
        } catch (SQLException e) {
            sn.setType(null);
        }
        try {
            sn.setTypeChild(rs.getShort(su + "type_child"));
        } catch (SQLException e) {
            sn.setTypeChild(null);
        }
        try {
            sn.setCdate(rs.getTimestamp(su + "cdate"));
        } catch (SQLException e) {
            sn.setCdate(null);
        }
        

        return sn;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, SysEmailHistory sn) {
        par.put("eid",sn.getEid());
        par.put("toEmail", sn.getToEmail());
        par.put("value", sn.getValue());
        par.put("type",sn.getType());
        par.put("type_child",sn.getTypeChild());
        par.put("cdate", sn.getCdate());

        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, SysEmailHistory sn) {
        args.put("eid",sn.getEid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final SysEmailHistory sn) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(sn),
                        new String[] { "eid" });
                parsePS(ps, sn);
                return ps;
            }

        };
    }

    private static String parseSQL(SysEmailHistory sn) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into sys_email_history(");
        int count = 0;
        if (null != sn.getToEmail()) {
            sql.append("toEmail,");
            count++;
        }

        if (null != sn.getValue()) {
            sql.append("value,");
            count++;
        }

        if (null != sn.getType()) {
            sql.append("type,");
            count++;
        }
        
        if (null != sn.getTypeChild()) {
            sql.append("type_child,");
            count++;
        }
        if (null != sn.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, SysEmailHistory sn)
        throws SQLException {
        int count = 1;
        if (null != sn.getToEmail()) {
            ps.setString(count++, sn.getToEmail());
        }

        if (null != sn.getValue()) {
            ps.setString(count++, sn.getValue());
        }

        if (null != sn.getType()) {
            ps.setShort(count++,sn.getType());
        }
        if (null != sn.getTypeChild()) {
            ps.setShort(count++, sn.getTypeChild());
        }
        if (null != sn.getCdate()) {
            ps.setTimestamp(count++, sn.getCdate());
        }
    

    }

}
